﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WG计划管理工具
{
    public partial class frmSQSelector : Form
    {
        public bool bCancel = true ;
        public List<DataForReturn> dfrlst;
        
        //默认计划查询语句
        string strDefaultSelectString = "select b.申请单号,b.申请项次,b.合同号,b.型号规格,b.申请数量,b.总金额,备注 from wgsqa a,wgsqb b where a.申请单号=b.申请单号 and b.已审核=0 and a.单据状态=1";

        public struct DataForReturn
        {
            public string[] strPPNo;                //申请单号
            public string[] strPX;                    //申请项次
            public string strModel;              //型号规格
            public int iQuantity;                  //申请数量
            public string strMemo;              //备注
            public string strContractNo;      //合同号
        }

        public frmSQSelector()
        {
            InitializeComponent();
        }

        private int GetInt(object tmpobj)
        {
            int r = 0;
            try
            {
                r= Convert.ToInt32(tmpobj);
            }
            catch
            {
                r= 0;
            }
            if (r < 0)
            {
                r = 0;
            }
            return r;
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = strDefaultSelectString;
            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            if (txtContractNo.Text.Trim().Length > 0)
            {
                strTmpSelectString += " and ((select count(*) from wgsqht e,scjhb_table f where e.生产计划号=f.jhb001 and e.生产计划项次=f.jhb002 and e.申请单号=b.申请单号 and e.申请项次=b.申请项次 and jhb024 like '" + txtContractNo.Text.Trim() + "')>0 or 合同号 like '"+txtContractNo .Text .Trim ()+"')";
            }
            if (txtModel.Text.Trim().Length > 0)
            {
                strTmpSelectString += " and b.型号规格 like '" + txtModel.Text.Trim() + "'";
            }
            if (txtPPNo.Text.Trim().Length > 0)
            {
                strTmpSelectString += " and b.申请单号 like '" + txtPPNo.Text.Trim() + "'";
            }
            strTmpSelectString += " order by b.申请单号";
            DataTable tmpdt = sqt.GetDataTable(strTmpSelectString);

            if (tmpdt.Rows.Count == 0)
            {
                dgv1.DataSource = null;
                return;
            }
            dgv1.DataSource = tmpdt;
            txtAllCount.Text = dgv1.Rows.Count.ToString();
        }

        private void dgv1_SelectionChanged(object sender, EventArgs e)
        {
            txtSelectedCount.Text = dgv1.SelectedRows.Count.ToString();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtContractNo.Clear();
            txtPPNo.Clear();
            txtModel.Clear();
            dgv1.DataSource = null;
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                foreach (DataGridViewRow tmpdgvr in dgv1.Rows)
                {
                    tmpdgvr.Selected = true;
                }
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            dfrlst = BuildDataForReturn();
            bCancel = false;
            this.Close();
        }

        private List <DataForReturn> BuildDataForReturn()
        {
            List<DataForReturn> tmpdfrlst = new List<DataForReturn>();
            DataForReturn dfr;
            if (dgv1.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                {
                    dfr = new DataForReturn();
                    string[] strPPNo = new string[1];
                    strPPNo[0] = GetString(dgv1.SelectedRows[i].Cells["申请单号"].Value);
                    dfr.strPPNo = strPPNo;
                    string[] strPX = new string[1];
                    strPX[0]  = GetString(dgv1.SelectedRows[i].Cells["申请项次"].Value);
                    dfr.strPX = strPX;
                    dfr.strModel =GetString ( dgv1.SelectedRows[i].Cells["型号规格"].Value);
                    dfr.iQuantity = GetInt(dgv1.SelectedRows[i].Cells["申请数量"].Value);
                    dfr.strMemo =GetString ( dgv1.SelectedRows[i].Cells["备注"].Value);
                    dfr.strContractNo =GetString (dgv1.SelectedRows[i].Cells["合同号"].Value);
                    tmpdfrlst.Add(dfr);
                }
            }
            return tmpdfrlst;
        }
    }
}